﻿// ***********************************************************************
// Assembly         : Tx.OCC
// Author           : tianteng
// Created          : 08-12-2021
//
// Last Modified By : tianteng
// Last Modified On : 08-12-2021
// ***********************************************************************
// <copyright file="OccInteractiveContextEx.cs" company="北京腾雪科技有限责任公司">
//     Copyright © 北京腾雪科技有限责任公司 2022
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Collections.Generic;
using Occt;

namespace Tx.OCC
{
    /// <summary>
    /// Class OccInteractiveContextEx.
    /// </summary>
    public static class OccInteractiveContextEx
    {
        /// <summary>
        /// Gets the detected shape from context.
        /// </summary>
        /// <param name="aisContext">The ais context.</param>
        /// <returns>TopoDS_Shape.</returns>
        public static TopoDS_Shape GetDetectedShapeFromContext(this AIS_InteractiveContext aisContext)
        {
            var owner = aisContext.DetectedOwner;
            if (owner.IsNull)
                return null;

            var brepOwner = owner as StdSelect_BRepOwner;
            if (brepOwner.IsNull || !brepOwner.HasShape)
                return null;

            return brepOwner.Shape;
        }

        /// <summary>
        /// Picks from context.
        /// </summary>
        /// <param name="aisContext">The ais context.</param>
        /// <param name="points">The points.</param>
        /// <param name="bAllowOverlapDetection">if set to <c>true</c> [b allow overlap detection].</param>
        /// <param name="theView">The view.</param>
        /// <param name="interactives">The interactives.</param>
        /// <param name="shapes">The shapes.</param>
        /// <returns>System.Int32.</returns>
        public static int PickFromContext(AIS_InteractiveContext aisContext, TColgp_Array1OfPnt2d points,  bool bAllowOverlapDetection, V3d_View theView, List<AIS_InteractiveObject> interactives,
            List<TopoDS_Shape> shapes)
        {

            var selector = aisContext.MainSelector;
            selector.AllowOverlapDetection(bAllowOverlapDetection);
            selector.Pick(points, theView);

            return _PickFromSelector(selector, interactives, shapes);
        }

        /// <summary>
        /// Picks from context.
        /// </summary>
        /// <param name="aisContext">The ais context.</param>
        /// <param name="theXPMin">The xp minimum.</param>
        /// <param name="theYPMin">The yp minimum.</param>
        /// <param name="theXPMax">The xp maximum.</param>
        /// <param name="theYPMax">The yp maximum.</param>
        /// <param name="bAllowOverlapDetection">if set to <c>true</c> [b allow overlap detection].</param>
        /// <param name="theView">The view.</param>
        /// <param name="interactives">The interactives.</param>
        /// <param name="shapes">The shapes.</param>
        /// <returns>System.Int32.</returns>
        public static int PickFromContext(AIS_InteractiveContext aisContext, int theXPMin, int theYPMin, int theXPMax, int theYPMax,
            bool bAllowOverlapDetection, V3d_View theView, List<AIS_InteractiveObject> interactives, List<TopoDS_Shape> shapes)
        {
            var selector = aisContext.MainSelector;
            selector.AllowOverlapDetection(bAllowOverlapDetection);
            selector.Pick(theXPMin, theYPMin, theXPMax, theYPMax, theView);

            return _PickFromSelector(selector, interactives, shapes);
        }

        /// <summary>
        /// Picks from selector.
        /// </summary>
        /// <param name="selector">The selector.</param>
        /// <param name="interactives">The interactives.</param>
        /// <param name="shapes">The shapes.</param>
        /// <returns>System.Int32.</returns>
        private static int _PickFromSelector(SelectMgr_ViewerSelector selector, List<AIS_InteractiveObject> interactives, List<TopoDS_Shape> shapes)
        {
            int count = 0;
            for (int aPickIter = 1; aPickIter <= selector.NbPicked; ++aPickIter)
            {
                var owner = selector.Picked(aPickIter);
                if (owner == null || !owner.HasSelectable) // || !filters.IsOk (owner))
                    continue;

                count++;

                if (interactives != null)
                {
                    var interactive = owner.Selectable as AIS_InteractiveObject;
                    if (interactive != null)
                        interactives.Add(interactive);
                }

                if (shapes != null)
                {
                    var brepOwner = owner as StdSelect_BRepOwner;
                    if (brepOwner != null && brepOwner.HasShape)
                        shapes.Add(brepOwner.Shape);
                }
            }
            return count;
        }

        /// <summary>
        /// Sets the rubberband points.
        /// </summary>
        /// <param name="wntWindow">The WNT window.</param>
        /// <param name="rubberband">The rubberband.</param>
        /// <param name="pointList">The point list.</param>
        public static void SetRubberbandPoints(WNT_Window wntWindow, AIS_RubberBand rubberband, List<Graphic3d_Vec2i> pointList)
        {
            wntWindow.Size(out int width, out int height);
            rubberband.ClearPoints();
            foreach (var point in pointList)
            {
                rubberband.AddPoint(new Graphic3d_Vec2i(point.x, height - point.y));
            }
        }
    }
}
